#include "ViewsContainer.h"

using namespace std;

///<************************************************************************************************************>
///<************************************* region CONSTANTS DEFINITION ******************************************>
///<************************************************************************************************************>

const QString CIGui::ViewsContainer::STYLE_SHEET = QString ("QToolBar {"
                                                             "padding-top: 0px;"
                                                             "border: 1px solid gray;"
                                                             "border-radius: 5px;"
                                                             "spacing: 2px;" 
                                                             + GENERAL_BACKGROUND +
                                                         "}"
                                                         );
/***************************************************************************************************************
************************************** endregion CONSTANTS DEFINITION ******************************************
****************************************************************************************************************/






/// <summary>
/// Constructor
/// </summary>
/// <param name="v"> A reference of the vector containing the 3D views </param>
/// <param name="parent"> The widget parent </param>
CIGui::ViewsContainer::ViewsContainer (vector<CIView::View*>& v, QWidget *parent) : QFrame (parent), views(v), viewsHeaders(4) {
    //setMinimumSize (360, 360);
    setSizePolicy (QSizePolicy (QSizePolicy::Expanding, QSizePolicy::Expanding));
    setStyleSheet (STYLE_SHEET);

    hasViewFullScreen = false;                                    //TODO -> Look at fullscreen-selection again
    
    CreateActions();                //Creates the actions
    CreateLayout();                 //Creates the container's layouts
    CreateContent();                //Creates the container's content widget    

    //setLineWidth(3);
    //setMidLineWidth(3);
    //setFrameStyle (QFrame::Shape::StyledPanel | QFrame::Shadow::Raised);
}

/// <summary>
/// Destructor
/// </summary>
CIGui::ViewsContainer::~ViewsContainer() {
    //Deleting layouts
    delete gridLayout;             //The container grid layout

    //Deleting views separators
    delete verticalSeparator;      //The views vertical separator
    delete horizontalSeparator;    //The views horizontal separator

    //Deleting views
    vector<CIView::View*>::iterator v_it = views.begin();
    while (v_it != views.end())
        delete (*v_it++);

    //Deleting Views'Headers
    vector<ViewHeader*>::iterator h_it = viewsHeaders.begin();
    while (h_it != viewsHeaders.end())
        delete (*h_it++);
}






///<************************************************************************************************************>
///<****************************************** region Public SLOTS *********************************************>
///<************************************************************************************************************>

/// <summary>
/// Method called to show (or don't show) the specified view in fullscreen mode
/// </summary>
/// <param name="v"> The view to show </param>
void CIGui::ViewsContainer::ToggleViewFullScreenMode (CIView::View* view) {
    bool b = hasViewFullScreen;
    hasViewFullScreen = !b;

    //Changing visibility of all views different from view
    for (unsigned int i = 0; i < views.size(); i++) 
        if (views[i] != view) {
            views[i]->setVisible (b);               //Changes visibility of  the view
            viewsHeaders[i]->setVisible (b);        //Changes visibility of the view's header
        }    

    //Changing visibility of  separators
    verticalSeparator->setVisible (b);
    horizontalSeparator->setVisible (b);
}
/***************************************************************************************************************
******************************************* endregion Public SLOTS *********************************************
****************************************************************************************************************/








///<************************************************************************************************************>
///<**************************************** region Utility Methods ********************************************>
///<************************************************************************************************************>

/// <summary>
/// Method called to create the actions
/// </summary>
void CIGui::ViewsContainer::CreateActions() {
    //TODO
}

/// <summary>
/// Method called to create the container's content
/// </summary>
void CIGui::ViewsContainer::CreateContent() {
    //Creating views'separators
    verticalSeparator = new Separator (this, Separator::VERTICAL);            //Creates the views vertical separator                
    horizontalSeparator = new Separator (this, Separator::HORIZONTAL);        //Creates the views horizontal separator
    
    //Creating views'headers
    for (unsigned int i = 0; i < 4; i++)
        viewsHeaders[i] = new ViewHeader (this, views[i]);  

    //Adding the views, views'headers and separators to the gridLayout
    gridLayout->addWidget (views[0], 1, 0);    
    gridLayout->addWidget (views[1], 4, 0);
    gridLayout->addWidget (views[2], 4, 2);
    gridLayout->addWidget (views[3], 1, 2);
    gridLayout->addWidget (viewsHeaders[0], 0, 0);
    gridLayout->addWidget (viewsHeaders[3], 0, 2);
    gridLayout->addWidget (viewsHeaders[1], 3, 0);
    gridLayout->addWidget (viewsHeaders[2], 3, 2);
    gridLayout->addWidget (verticalSeparator, 0, 1, -1, 1);
    gridLayout->addWidget (horizontalSeparator, 2, 0, 1, -1);
}

/// <summary>
/// Method called to create the container's layout
/// </summary>
void CIGui::ViewsContainer::CreateLayout() {
    gridLayout = new QGridLayout (0, 5, 3, 0, 1);        //The views layout, to create a 5x3 table
    gridLayout->setMargin (10);                          //Sets the views layout margins

    setLayout (gridLayout);
}
/***************************************************************************************************************
***************************************** endregion Utility Methods ********************************************
****************************************************************************************************************/
